//
//  KZAnswerClassWrittingView.m
//  lg_zhinengbeikao_english
//
//  Created by 朱凯 on 2019/1/9.
//  Copyright © 2019 zhukai. All rights reserved.
//

#import "KZAnswerClassWrittingView.h"

/**
 选照片的cell
 */
@interface KZSelectPictureCell: UICollectionViewCell
@end
@implementation KZSelectPictureCell
{
    UIImageView *_addIconView;
}
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        _addIconView = [UIImageView new];
        _addIconView.backgroundColor = kBlueColor;
        [self.contentView addSubview:_addIconView];
        [_addIconView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.contentView);
        }];
    }
    return self;
}

@end
/**
 展示照片的cell
 */
@interface KZShowPictureCell: UICollectionViewCell
@property (nonatomic, copy) void (^deleteCallback)(void);
@end
@implementation KZShowPictureCell
{
    UIImageView *_pictureView;
    UIButton *_deleteButton;
}
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        _pictureView = [UIImageView new];
        _pictureView.backgroundColor = kRedColor;
        [self.contentView addSubview:_pictureView];
        [_pictureView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.contentView);
        }];
        
        _deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _deleteButton.backgroundColor = kBlackColor;
        [_deleteButton setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
        [_deleteButton addTarget:self action:@selector(delete) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:_deleteButton];
        [_deleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.mas_equalTo(W(20));
            make.right.top.equalTo(self.contentView);
        }];
    }
    return self;
}
- (void)delete {
    
    if (_deleteCallback) {
        _deleteCallback();
    }
}
@end

#pragma mark =========== 分隔 ===========
@implementation KZAnswerClassWrittingView
{
    KZShadowButton *_submitButton;
    
    UIView *_typeSelectButtonsView; //

    UIView *_buttonContentView;     // 按钮区域：清空，录音，提交
    
    UIView *_contentView1;          // 写作
    UIView *_contentView2;          // 录音
    UIView *_contentView3;          // 选照片
    
    UICollectionView *_collect;     // 列表
    NSMutableArray *_pictures;      // 已经选中的照片
}



- (void)setupUI {
    
    _submitButton = [[KZShadowButton alloc] init:^(UILabel *label) {
        label.text = @"提交";
    }];
    [self addSubview:_submitButton];
    [_submitButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(W(20));
        make.right.mas_equalTo(W(-20));
        make.bottom.mas_equalTo(H(-10));
        make.height.mas_equalTo(H(36));
    }];
    
    UILabel *numberLabel = [[UILabel alloc] init];
    numberLabel.text = [NSString stringWithFormat:@"%@.写作", @20];
    numberLabel.font = FONT(14);
    numberLabel.textAlignment = NSTextAlignmentLeft;
    numberLabel.textColor = [UIColor blackColor];
    [self addSubview:numberLabel];
    [numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(W(10));
        make.top.mas_equalTo(H(10));
    }];
   
    
    [self setupTypeSelectButtons];
    [self setupContent1Views];
    [self setupContent2Views];
    [self setupContent3Views];
}
// 选择作答方式
- (void)setupTypeSelectButtons {
    
    _typeSelectButtonsView = [UIView new];
    [self addSubview:_typeSelectButtonsView];
    [_typeSelectButtonsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self);
        make.height.mas_equalTo(H(50));
    }];
    
    NSArray *iconStrs = @[@"图片", @"录音", @"手写"];
    UIButton *last = nil;
    for (int i = 0; i < iconStrs.count; i ++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.backgroundColor = kBlueColor;
        button.tag = i;
        [button setImage:[UIImage imageNamed:iconStrs[i]] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:iconStrs[i]] forState:UIControlStateSelected];
        [button addTarget:self action:@selector(onSelectType:) forControlEvents:UIControlEventTouchUpInside];
        [_typeSelectButtonsView addSubview:button];
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.mas_equalTo(W(30));
            make.centerY.equalTo(self->_typeSelectButtonsView);
            if (last) {
                make.right.equalTo(last.mas_left).offset(W(-30));
            } else {
                make.right.mas_equalTo(W(-10));
            }
        }];
        last = button;
    }
}

// 写作区域
- (void)setupContent1Views {
    
    _contentView1 = [UIView new];
    [self addSubview:_contentView1];
    [_contentView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self);
        make.bottom.equalTo(self->_submitButton.mas_top);
        make.top.equalTo(self->_typeSelectButtonsView.mas_bottom);
    }];
    
    UIView *borderView = [UIView new];
    borderView.layer.cornerRadius = 2;
    borderView.layer.masksToBounds = true;
    borderView.layer.borderColor = [UIColor colorWithWhite:0.8 alpha:1].CGColor;
    borderView.layer.borderWidth = 1;
    [_contentView1 addSubview:borderView];
    [borderView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(W(10));
        make.right.mas_equalTo(W(-10));
        make.top.mas_equalTo(0);
        make.bottom.mas_equalTo(H(-10));
    }];
    
    UITextView *inputView = [UITextView new];
    [_contentView1 addSubview:inputView];
    [inputView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(borderView).insets(UIEdgeInsetsMake(5, 5, 5, 5));
    }];
}
// 录音区域
- (void)setupContent2Views {
    
    _contentView2 = [UIView new];
    _contentView2.hidden = true;
    [self addSubview:_contentView2];
    [_contentView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self->_contentView1);
    }];

    // 录音等按钮
    [self setupBottomButtons];
    
    UIView *borderView = [UIView new];
    borderView.layer.cornerRadius = 2;
    borderView.layer.masksToBounds = true;
    borderView.layer.borderColor = [UIColor colorWithWhite:0.8 alpha:1].CGColor;
    borderView.layer.borderWidth = 1;
    [_contentView2 addSubview:borderView];
    [borderView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(W(10));
        make.right.mas_equalTo(W(-10));
        make.top.mas_equalTo(0);
        make.bottom.equalTo(self->_buttonContentView.mas_top);
    }];
    
    UITextView *inputView = [UITextView new];
    [_contentView2 addSubview:inputView];
    [inputView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(borderView).insets(UIEdgeInsetsMake(5, 5, 5, 5));
    }];

}
// 底部录音等按钮
- (void)setupBottomButtons {
    
    _buttonContentView = [UIView new];
    [_contentView2 addSubview:_buttonContentView];
    [_buttonContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self->_contentView2);
        make.height.mas_equalTo(H(80));
    }];
    
    UIButton *recordButton = [UIButton buttonWithType:UIButtonTypeCustom];
    recordButton.layer.cornerRadius = W(30);
    recordButton.layer.masksToBounds = true;
    recordButton.backgroundColor = kRedColor;
    recordButton.titleLabel.font = FONT(18);
    [recordButton setTitle:@"录音" forState:UIControlStateNormal];
    [recordButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [recordButton addTarget:self action:@selector(record) forControlEvents:UIControlEventTouchUpInside];
    [_buttonContentView addSubview:recordButton];
    [recordButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(W(60));
        make.center.equalTo(self->_buttonContentView);
    }];
    
    UIButton *clearButton = [UIButton buttonWithType:UIButtonTypeCustom];
    clearButton.titleLabel.font = FONT(14);
    [clearButton setTitle:@"清空" forState:UIControlStateNormal];
    [clearButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [clearButton addTarget:self action:@selector(clear) forControlEvents:UIControlEventTouchUpInside];
    [_buttonContentView addSubview:clearButton];
    [clearButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(W(30));
        make.centerY.equalTo(recordButton);
    }];
    
    UIButton *submitButton = [UIButton buttonWithType:UIButtonTypeCustom];
    submitButton.titleLabel.font = FONT(14);
    [submitButton setTitle:@"提交" forState:UIControlStateNormal];
    [submitButton setTitleColor:kBlueColor forState:UIControlStateNormal];
    [submitButton addTarget:self action:@selector(submit) forControlEvents:UIControlEventTouchUpInside];
    [_buttonContentView addSubview:submitButton];
    [submitButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(W(-30));
        make.centerY.equalTo(recordButton);
    }];
}
// 图片区域
- (void)setupContent3Views {
    
    _pictures = [NSMutableArray new];
    
    _contentView3 = [UIView new];
    _contentView3.hidden = true;
    [self addSubview:_contentView3];
    [_contentView3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self->_contentView1);
    }];
    
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing = W(10);
    layout.minimumInteritemSpacing = W(10);
    layout.sectionInset = UIEdgeInsetsMake(W(10), W(10), W(10), W(10));
    layout.itemSize = CGSizeMake((SCREEN_WIDTH - W(10 * 5)) / 4, (SCREEN_WIDTH - W(10 * 5)) / 4);
    
    _collect = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    _collect.backgroundColor = UIColor.whiteColor;
    _collect.delegate = self;
    _collect.dataSource = self;
    [_contentView3 addSubview:_collect];
    [_collect mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self->_contentView3);
        make.top.equalTo(self->_typeSelectButtonsView.mas_bottom);
    }];
    [_collect registerClass:[KZShowPictureCell class] forCellWithReuseIdentifier:@"pictureCell"];
    [_collect registerClass:[KZSelectPictureCell class] forCellWithReuseIdentifier:@"selectCell"];
}
#pragma mark - 事件
- (void)onSelectType:(UIButton *)button {
    
    if (button.selected == true) {
        return;
    }
    
    for (int i = 0; i < _typeSelectButtonsView.subviews.count; i ++) {
        UIButton *btn = _typeSelectButtonsView.subviews[i];
        btn.selected = false;
    }
    button.selected = true;
    switch (button.tag) {
        case 0:
        {
            _contentView1.hidden = true;
            _contentView2.hidden = true;
            _contentView3.hidden = false;
        }
            break;
        case 1:
        {
            _contentView1.hidden = true;
            _contentView2.hidden = false;
            _contentView3.hidden = true;
        }
            break;
        case 2:
        {
            _contentView1.hidden = false;
            _contentView2.hidden = true;
            _contentView3.hidden = true;
        }
            break;
            
        default:
            break;
    }
}
- (void)record {
    
}
- (void)clear {
    
}
- (void)submit {
    
}

#pragma mark - UICollectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _pictures.count + 1;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.item < _pictures.count) {
        KZShowPictureCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pictureCell" forIndexPath:indexPath];
        cell.deleteCallback = ^{
            [self->_pictures removeObjectAtIndex:indexPath.item];
            [self->_collect reloadData];
        };
        return cell;
    } else {
        KZSelectPictureCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"selectCell" forIndexPath:indexPath];
        return cell;
    }
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [collectionView deselectItemAtIndexPath:indexPath animated:false];
    if (indexPath.item < _pictures.count) {
        NSLog(@"点击了图片");
    }
    else {
        [_pictures addObject:@""];
        [_collect reloadData];
    }
}



#pragma mark - setter
- (void)setModel:(KZExamItemModel *)model {
    
    _model = model;
    [self setupUI];
}

@end
